package org.springframework.howelly.model;

import java.io.Serializable;
import java.math.RoundingMode;
import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

import org.springframework.binding.message.MessageContext;
import org.springframework.howelly.utils.Constants;
import org.springframework.howelly.validators.GastoValidator;
import org.springframework.webflow.execution.RequestContext;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Ingreso extends Gasto implements Serializable {

    private static final long serialVersionUID = -337893311697346346L;

    public Ingreso() {

    }

    public Ingreso(Obra obra, User user) {
	super.obra = obra;
	super.user = user;
	Calendar calendar = Calendar.getInstance();
	setDate(calendar.getTime());
	calendar.add(Calendar.DAY_OF_MONTH, 1);
	expenseType = Constants.INGRESO;
	factura = "--------";
    }

    @Override
    public Gasto calculateSaldosInGasto(Obra obra, RequestContext context) {
	// el saldo es sobre el bruto
	setSaldoParcial(obra.getSaldo().add(getPrice()).setScale(2, RoundingMode.HALF_DOWN));
	// obra.setSaldo(getSaldoParcial());
	setObra(obra);
	return this;
    }

    public void validateIngresoObra(MessageContext context) {
	GastoValidator.validateIngresoObra(context, this);
    }
}
